package com.bbx.taxi.client.Task;

import java.util.ArrayList;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;

import com.bbx.api.sdk.model.passanger.MyOrder;
import com.bbx.api.sdk.model.passanger.Token;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.MyOrderList;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.MyOrders;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.passeger.conn.MyOrderNet;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.taxi.client.Task.MyOrderingTask.OnOrderingFinishListener;
import com.bbx.taxi.client.Task.MyOrderingTask.OnOrderingFinishToMessageListener;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;
import com.bbx.taxi.client.Activity.Menu.MyOrderActivity;
import com.bbx.taxi.client.DB.MyOrder.MyOrderDB;

public class MyOrderTask {
	private Context context;
	public static MyOrderTask instance;
	public MyOrderTask_All mMyOrderTask_All;
	private MyOrderDB db_myorder;// 我的订单数据库
	private ArrayList<MyOrderList> list;
	String jmsg_data;
	JSONObject data;
	int type;
	int online_type;
	public interface OnOrderFinishListener {
		public void onOrderFinish(ArrayList<MyOrderList> list);
	}
	public interface OnOrderFinishToMessageListener {
		public void onOrderFinishToMessage(ArrayList<MyOrderList> list,int online_type,String jmsg_data,JSONObject data);
	}
	static OnOrderFinishToMessageListener mOnOrderFinishToMessageListener;
	static ArrayList<OnOrderFinishListener> mOnOrderFinishListener = new ArrayList<MyOrderTask.OnOrderFinishListener>();

	public static void setOnOrderFinishListener(OnOrderFinishListener l) {
		if (!mOnOrderFinishListener.contains(l))
			mOnOrderFinishListener.add(l);
	}
	public static void removeOnOrderFinishListener(OnOrderFinishListener l) {
		mOnOrderFinishListener.remove(l);
	}
	public static void setOnOrderFinishToMessageListener(OnOrderFinishToMessageListener l) {
		mOnOrderFinishToMessageListener=l;
	}
	public static MyOrderTask getInstance(Context context,int count) {
		instance = new MyOrderTask(context,count);
		return instance;
	}
	public static MyOrderTask getInstance(Context context,int count,int online_type,String jmsg_data,JSONObject data) {
		instance = new MyOrderTask(context,count,online_type,jmsg_data,data);
		return instance;
	}
	public static MyOrderTask getInstance(Context context) {
		instance = new MyOrderTask(context);
		return instance;
	}
	public MyOrderTask(Context context,int count) {
		this.context = context;
		db_myorder = new MyOrderDB();
		getMyOrder(count);
	}
	public MyOrderTask(Context context,int count,int online_type,String jmsg_data,JSONObject data) {
		this.context = context;
		db_myorder = new MyOrderDB();
		this.jmsg_data=jmsg_data;
		this.data=data;
		this.online_type=online_type;
		getMyOrder(count);
	}
	public MyOrderTask(Context context) {
		this.context = context;
		db_myorder = new MyOrderDB();
		getMyOrder(MyOrderActivity.num_all);
	}
	/**
	 * 获取全部订单
	 */
	public void getMyOrder(int count){
		list=null;
		Token token = SharedPreEncryptUtil.getStringEncryptValue(context,
				SharedPreEncryptUtil.TokenAndUid, "");
		if(token!=null&&token.getUid()!=null&& token.getAccess_token()!=null
				&&!token.getUid().equals("")&&!token.getAccess_token().equals("")){
		MyOrder myOrder = new MyOrder(context);
		myOrder.uid = token.getUid();
		myOrder.access_token = token.getAccess_token();
		myOrder.start_id = 0;
		myOrder.count = count;
		myOrder.direction = 0;
		mMyOrderTask_All = new MyOrderTask_All(context, myOrder);
		mMyOrderTask_All.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		}
	}

	/**
	 * 进行中的订单
	 */
	private class MyOrderTask_All extends BaseAsyncTask {

		private MyOrder myOrder;

		public MyOrderTask_All(Context context, MyOrder myOrder) {
			super(context,false,0);
			this.myOrder = myOrder;
		}

		@Override
		public void onSuccess(int status,Object object) {
			if (object != null) {
				MyOrders myOrders = (MyOrders) object;
				if (myOrders.getList() != null && !myOrders.getList().isEmpty()) {
					list = myOrders.getList();
					// 存储数据库
					db_myorder.onWriteDB(
							myOrders.getList(),true,null);
					mHandler.sendEmptyMessage(BaseBBXActivity.MSG_ORDER_WRITE);
				} else {
					if (!mOnOrderFinishListener.isEmpty()) {
						for (int i = 0; i < mOnOrderFinishListener.size(); i++) {
							mOnOrderFinishListener.get(i).onOrderFinish(null);
						}
				      }
					if (mOnOrderFinishToMessageListener!=null) {
						mOnOrderFinishToMessageListener.onOrderFinishToMessage(null,online_type, null,null);
				      }
					// 删除数据库
					db_myorder.onDelete();
				}
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			if (!mOnOrderFinishListener.isEmpty()) {
				for (int i = 0; i < mOnOrderFinishListener.size(); i++) {
					mOnOrderFinishListener.get(i).onOrderFinish(null);
				}
		      }
			if (mOnOrderFinishToMessageListener!=null) {
				mOnOrderFinishToMessageListener.onOrderFinishToMessage(null,online_type, null,null);
		      }
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(myOrder).getJson();
			return new MyOrderNet(context, json);
		}
	}
	Handler mHandler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
			//进行中的订单写入完成
			case BaseBBXActivity.MSG_ORDER_WRITE:
				if (!mOnOrderFinishListener.isEmpty()) {
					for (int i = 0; i < mOnOrderFinishListener.size(); i++) {
						mOnOrderFinishListener.get(i).onOrderFinish(list);
					}
			      }
				if (mOnOrderFinishToMessageListener!=null) {
					mOnOrderFinishToMessageListener.onOrderFinishToMessage(list,online_type,jmsg_data,data);
			      }
				break;
			}
		}
	};
}
